import Cookie from 'js-cookie';

// 当前主题缓存
const themeCache = {}
// 当前主题
let currentTheme = null;

/** 主题 */
const themes = {
    // 默认主题
    async default () {
        if (!themeCache.default) {
            themeCache.default = await import('@/assets/theme/default/index.skin.less');
        }
    },
    // 黑暗主题
    async dark () {
        if (!themeCache.dark) {
            themeCache.dark = await import('@/assets/theme/dark/index.skin.less');
        }
    }
}

const initTheme = () => {
    changeTheme();
}

const changeTheme = (setting = {
    themeName: "default"
}) => {
    // 将skin class设置到document上
    document.body.className = `skin-${setting.themeName}`;
    Cookie.set('skinName', setting.themeName); // 将主题配置放在cookie里，下次打开浏览器还可以还原
    // 引入主题css
    if (themes[setting.themeName]) {
        themes[setting.themeName]();
    }
}

export {
    initTheme,
    changeTheme
}